package fr.janua.client;

import java.util.Date;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import fr.janua.shared.UserDTO;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Test_GWT_Session implements EntryPoint {

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final GreetingServiceAsync greetingService = GWT
			.create(GreetingService.class);

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		RootPanel root = RootPanel.get("root");
		
		
		// widget pour la connexion (sera sur la gauche)
		final TextBox tBox_login = new TextBox();
		final TextBox tBox_pswd = new TextBox();
		Button button_login = new Button("login");
		Button button_logout = new Button("logout");
		
		// handler pour boutons
		button_login.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				String login = tBox_login.getText();
				String pswd = tBox_pswd.getText();
				login(login, pswd);
			}
		});
		button_logout.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				tBox_login.setText("");
				tBox_pswd.setText("");
				logout();
			}
		});
		
		// html5
		tBox_login.getElement().setAttribute("placeholder", "login");
		tBox_pswd.getElement().setAttribute("placeholder", "password");
		
		// label pour afficher les infos
		HTML html_info = new HTML("...");
		html_info.addStyleName("box-info");
		
		
		HorizontalPanel hPanel_wrapper = new HorizontalPanel();
		
		VerticalPanel vPanel_fieldLogin = new VerticalPanel();
		vPanel_fieldLogin.add(tBox_login);
		vPanel_fieldLogin.add(tBox_pswd);
		vPanel_fieldLogin.add(button_login);
		vPanel_fieldLogin.add(button_logout);
		
		hPanel_wrapper.add(vPanel_fieldLogin);
		hPanel_wrapper.add(html_info);
		
		root.add(hPanel_wrapper);
	}
	
	
	private void login(String login, String pswd){
		LoginService.Util.getInstance().loginServer(login, pswd, new AsyncCallback<UserDTO>() {
					@Override
					public void onSuccess(UserDTO result) {
						if (result.isLoggedIn()) {
							RootPanel.get().clear();
							// load the next app page

							// set session cookie for 1 day expiry.
							String sessionID = result.getSessionId();
							final long DURATION = 1000 * 60 * 60 * 24 * 1;
							Date expires = new Date(System.currentTimeMillis() + DURATION);
							Cookies.setCookie("sid", sessionID, expires, null, "/", false);
						} else {
							Window.alert("Access Denied. Check your user-name and password.");
						}

					}

					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Access Denied. Check your user-name and password.");
					}
				});
	}
	
	private void logout(){
		
	}
	
	
	
}
